/* GSCommunicationApepProcessor.java
 *
 * Project/Product: GWBS2009
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   Administrator	2009-3-19	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2009
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package com.hitrust.gs.business;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.regex.Pattern;

import system.LoginContext;
import system.base.Warehouse;

import com.apep.connect.APEPRequest;
import com.apep.connect.APEPResponse;
import com.apep.connect.APEPServiceException;
import com.apep.lib.compress.GZIP;
import com.apep.lib.security.Base64;
import com.apep.lib.xml.SampleElement;
import com.apep.lib.xml.SampleXMLDocument;
import com.hitrust.gs.bean.GSInOrOutStorageBean;
import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSOrderAnaDetailAndCategoryBean;
import com.hitrust.gs.bean.GSOrderApplyBean;
import com.hitrust.gs.bean.GSOrderDetailBean;
import com.hitrust.gs.bean.GSProductApepBean;
import com.hitrust.gs.bean.GSProductStorageApepBean;
import com.hitrust.gs.dao.GSManufactureDAO;
import com.hitrust.gs.dao.GSOrderApplyDao;
import com.hitrust.gs.dao.GSOrderDetailDAO;
import com.hitrust.gs.dao.GSProductStorageDao;
import com.hitrust.gs.integration.socket.GSApepClient;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.lib.db.GSBaseDAO;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.service.GSBaseProcesser;
import com.hitrust.gs.util.DefineDic;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.lang.StringUtils;
import com.hitrust.gs.vo.GSOrderReceiveQueryVO;

import framework.core.ConstDefine;
import framework.core.DBConnection;
import framework.core.DBEngine;
import framework.core.LogWriter;
/**
 * 与apep交互接口业务类
 * **/
public class GSCommunicationApepProcessor implements GSBaseProcesser {

	public static String ROOT="Order";
	public static String CUSID=GSSystemConfigurator.getParam("gs.purcode");
	public static String CUSNAME=ConstDefine.T_WAREE_HOUSE_NAME;
	public static String SIGN="\"";
	private GSProductStorageDao pdao=new GSProductStorageDao();

	public static  SampleElement getOrderDetailElement(GSOrderAnaDetailAndCategoryBean detailBean,LoginContext context,String applyCode){
		String currentdate=DateUtil.getCurrentDate("yyyy-MM-dd hh:mm:ss");
		SampleElement element=new SampleElement(ROOT);
		element.addChildElement(new SampleElement("CusCode",CUSID));		
		element.addChildElement(new SampleElement("CusName",GSBaseManager.getWareHoseNameByCode(detailBean.getGs_workcorp_id())));
		element.addChildElement(new SampleElement("CusOrderItemCode",detailBean.getGs_order_detail_id()));
		element.addChildElement(new SampleElement("ProviderCode",detailBean.getCode()));
		element.addChildElement(new SampleElement("ProductID",detailBean.getApep_code()));
		element.addChildElement(new SampleElement("CreateTime",currentdate));
		element.addChildElement(new SampleElement("SubmitPerson",context.getLoginUserId()));
		element.addChildElement(new SampleElement("Qty",String.valueOf(detailBean.getGs_order_count())));
		element.addChildElement(new SampleElement("CusItemCode",detailBean.getGs_concentricity_id()));
		element.addChildElement(new SampleElement("Remark",StringUtils.escapeXML(detailBean.getGs_notes())));
		element.addChildElement(new SampleElement("SendMode",GSBaseManager.getDictioanryName(detailBean.getGs_deliver_mode())));
		element.addChildElement(new SampleElement("PayMode",GSBaseManager.getDictioanryName(detailBean.getGs_balance_mode_order())));
		element.addChildElement(new SampleElement("SendRequire",detailBean.getGs_deliver_goods_date()+" "+GSBaseManager.getDeliverGoodsTime(detailBean.getGs_deliver_goods_time())));
		if(context.getLoginWarehouseCode().startsWith("000017")){
			element.addChildElement(new SampleElement("Address",StringUtils.escapeXML(detailBean.getGs_deliver_goods_addr()+"("+context.getLoginUserName()+")")));
		}
		element.addChildElement(new SampleElement("Address",StringUtils.escapeXML(detailBean.getGs_deliver_goods_addr())));
		element.addChildElement(new SampleElement("LinkPerson",StringUtils.escapeXML(detailBean.getGs_order_contact_name())));
		element.addChildElement(new SampleElement("LinkPhone",detailBean.getGs_order_contact_phone()));
		element.addChildElement(new SampleElement("PurPrice",String.valueOf(detailBean.getGs_goods_unitprice())));
		String property=detailBean.getAppender_property();
		StringBuffer temp=new StringBuffer();
		Pattern p = Pattern.compile(";|；");
		if (property!=null&&!"".equals(property)) {
			String[] tstr=p.split(property);
			for (int tI = 0; tI < tstr.length; tI++) {
				String[] tmp=tstr[tI].split(":|：");
				if(tmp!=null&&tmp.length>1){
					temp.append("<"+tmp[0]+">"+tmp[1]+"</"+tmp[0]+">");
				}else if(tmp!=null){
					temp.append("<其他属性>"+tmp[0]+"</其他属性>");
				}
			}
		}
		element.addChildElement(new SampleElement("OtherProperty",StringUtils.escapeXML(temp.toString())));
		element.addChildElement(new SampleElement("ProductName",StringUtils.escapeXML(detailBean.getName_product())));
		element.addChildElement(new SampleElement("ProductModel",StringUtils.escapeXML(detailBean.getModel())));
		element.addChildElement(new SampleElement("ProductSpec",StringUtils.escapeXML(detailBean.getSpec())));
		element.addChildElement(new SampleElement("ProductUnit",detailBean.getProduct_unit()));
		StringBuffer cat=new StringBuffer();
		if(detailBean.getCategory_level().equals("1")){
			cat.append(detailBean.getCategory_code());
		}else if(detailBean.getCategory_level().equals("2")){
			cat.append(detailBean.getCategory_id1()+"-"+detailBean.getCategory_code());
		}else{
			cat.append(detailBean.getCategory_id1()+"-"+detailBean.getCategory_id2()+"-"+detailBean.getCategory_code());
		}
		//        cat.append(detailBean.getCategory_code()+"-"+detailBean.getCategory_id1()+"-"+detailBean.getCategory_id2());
		element.addChildElement(new SampleElement("ProductCatalog",cat.toString()));
		element.addChildElement(new SampleElement("Manufaturer",StringUtils.escapeXML(detailBean.getManufaturer())));
		element.addChildElement(new SampleElement("ProductTitle",StringUtils.escapeXML(detailBean.getProduct_brand())));
		element.addChildElement(new SampleElement("ProductArea",StringUtils.escapeXML(detailBean.getArea())));
		element.addChildElement(new SampleElement("ProductPic",""));
		element.addChildElement(new SampleElement("ProductPicNo",StringUtils.escapeXML(detailBean.getDrawing_piece_code())));
		element.addChildElement(new SampleElement("ProductKeepTerm",StringUtils.escapeXML(detailBean.getQuality_assure_period())));
		element.addChildElement(new SampleElement("ProductPicType",""));
		element.addChildElement(new SampleElement("IsUpdate",detailBean.getIs_update()));
		element.addChildElement(new SampleElement("ReplenishmentNo",applyCode));
		return element;
	}
	/**********************提交请求*****************************/

	//创建订单
	public static String submitOrderDetailNewToApep(GSOrderAnaDetailAndCategoryBean detailBean,LoginContext context,String applyeCode) throws Exception{
		APEPRequest req=new APEPRequest();
		req.setAction("CreateOrder");
		SampleElement element=getOrderDetailElement(detailBean, context,applyeCode);
		req.setData(element);
		GSApepClient cilent=new GSApepClient();
		APEPResponse res;
		try {
			res= cilent.sendToApep(req);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw e;
		}
		
		String tRs=res.toXml();
		GSLogWriter.info(tRs);
		return tRs;
	}

	//申沃签收
	public static String sendOrderDetailSignSw(String[] sign)throws Exception{
		APEPRequest req=new APEPRequest();
		req.setAction("ReceiveOrder");
		SampleElement element=new SampleElement(ROOT);
		element.addChildElement(new SampleElement("CusCode",CUSID));
		element.addChildElement(new SampleElement("CusOrderItemCode",sign[0]));
		element.addChildElement(new SampleElement("ReceiveTime",sign[2]));
		element.addChildElement(new SampleElement("ReceiveQty",String.valueOf(sign[1])));
		element.addChildElement(new SampleElement("ReceivePerson",sign[4]));
		element.addChildElement(new SampleElement("ReceiveCreateTime",getCurDT()));
		element.addChildElement(new SampleElement("Remark",sign[3]));
		req.setData(element);
		GSApepClient cilent=new GSApepClient();
		APEPResponse res= cilent.sendToApep(req);
		String tRs=res.toXml();
		GSLogWriter.info(res.toXml());
		return tRs;
	}


	//取消订单
	public static String cancelOrderDetailApep(String orderDetailId,String reason,LoginContext context) throws Exception{
		APEPRequest req=new APEPRequest();
		req.setAction("CancelOrder");
		String currentdate=DateUtil.getCurrentDate("yyyy-MM-dd hh:mm:ss");
		SampleElement element=new SampleElement(ROOT);
		element.addChildElement(new SampleElement("CusCode",CUSID));
		element.addChildElement(new SampleElement("CusOrderItemCode",orderDetailId));
		element.addChildElement(new SampleElement("CancelReason",StringUtils.escapeXML(reason)));
		element.addChildElement(new SampleElement("CancelTime",currentdate));
		element.addChildElement(new SampleElement("SubmitPerson",context.getLoginUserName()));
		req.setData(element);
		GSApepClient cilent=new GSApepClient();
		APEPResponse res= cilent.sendToApep(req);
		String tRs=res.toXml();
		GSLogWriter.info(tRs);
		return tRs;
	}

	//修改订单
	public static String modifyOrderDetailApep(GSOrderAnaDetailAndCategoryBean detailBean,LoginContext context) throws Exception{
		APEPRequest req=new APEPRequest();
		req.setAction("ModifyOrder");
		SampleElement element=getOrderDetailElement(detailBean, context,"");
		req.setData(element);
		GSApepClient cilent=new GSApepClient();
		APEPResponse res= cilent.sendToApep(req);
		String tRs=res.toXml();
		GSLogWriter.info(tRs);
		return tRs;
	}
	//拒绝补货申请
	//TODO ????
	public static String applyNoAggreApep(ArrayList list) throws Exception{
		APEPRequest req=new APEPRequest();
		req.setAction("");
		SampleElement element=null;
		req.setData(element);
		GSApepClient cilent=new GSApepClient();
		APEPResponse res= cilent.sendToApep(req);
		String tRs=res.toXml();
		GSLogWriter.info(tRs);
		return tRs;
	}
	//发送签收信息(子系统签收)
	public static void sendOrderDetailSign(SampleXMLDocument xmlDocument)throws Exception{
		APEPRequest req=new APEPRequest();
		req.setAction("ReceiveOrder");
		SampleElement element=new SampleElement(ROOT);
		element.addChildElement(new SampleElement("CusCode",CUSID));
		element.addChildElement(new SampleElement("CusOrderItemCode",xmlDocument.getValueNoNull("CusOrderItemCode")));
		element.addChildElement(new SampleElement("ReceiveTime",xmlDocument.getValueNoNull("ReceiveTime")));
		element.addChildElement(new SampleElement("ReceiveQty",xmlDocument.getValueNoNull("ReceiveQty")));
		element.addChildElement(new SampleElement("ReceivePerson",xmlDocument.getValueNoNull("ReceivePerson")));
		element.addChildElement(new SampleElement("ReceiveCreateTime",getCurDT()));
		element.addChildElement(new SampleElement("Remark",StringUtils.escapeXML(xmlDocument.getValueNoNull("ReceiveRemark"))));
		req.setData(element);
		GSApepClient cilent=new GSApepClient();
		APEPResponse res= cilent.sendToApep(req);
		GSLogWriter.info(res.toXml());
	}
	//发送签收信息
	public static String sendOrderDetailSign(GSOrderReceiveQueryVO vo)throws Exception{
		APEPRequest req=new APEPRequest();
		req.setAction("ReceiveOrder");
		SampleElement element=new SampleElement(ROOT);
		element.addChildElement(new SampleElement("CusCode",CUSID));
		element.addChildElement(new SampleElement("CusOrderItemCode",vo.orderDetailId));
		element.addChildElement(new SampleElement("ReceiveTime",vo.signin_date));
		element.addChildElement(new SampleElement("ReceiveQty",String.valueOf(vo.signinCount)));
		element.addChildElement(new SampleElement("ReceivePerson",vo.signin_person));
		element.addChildElement(new SampleElement("ReceiveCreateTime",getCurDT()));
		element.addChildElement(new SampleElement("Remark",StringUtils.escapeXML(vo.signin_explain)));
		req.setData(element);
		GSApepClient cilent=new GSApepClient();
		APEPResponse res= cilent.sendToApep(req);
		String tRs=res.toXml();
		GSLogWriter.info(res.toXml());
		return tRs;
	}
	/*****************************接收请求响应*************************************/
	//送货确认
	public APEPResponse processSendOrder(APEPRequest req){
		GSOrderDetailDAO tOrderDetailDAO=new GSOrderDetailDAO();
		Connection conn=tOrderDetailDAO.getConnetion();
		double tDetailCount = 0;
		APEPResponse res=new APEPResponse();
		try {
			conn.setAutoCommit(false);
			SampleXMLDocument xmlDocument=new SampleXMLDocument(req.toXml());
			ArrayList tList= xmlDocument.getDocuments(ROOT);
			ArrayList tempList = new ArrayList();
			for (int i = 0; i <tList.size(); i++) {
				xmlDocument=(SampleXMLDocument) tList.get(i);
				String orderDetailId=xmlDocument.getValueNoNull("CusOrderItemCode");
				//根据传入的每条订单明细送货确认信息的订单编号查询订单明细
				GSOrderDetailBean tOrderDetailBean = tOrderDetailDAO.queryOrderDetailByDetailId(conn,orderDetailId);
				if(tOrderDetailBean != null){
					tDetailCount = Double.parseDouble(xmlDocument.getValueNoNull("SendQty"));
					if(tOrderDetailBean.getGs_order_count() !=tDetailCount ){
						GSLogWriter.error(this,"订货数量不等于配送数量！");
					}
					tOrderDetailBean.setGs_order_detail_status(GSBaseConstants.ORDER_ORDER_CONFIRM);
					tOrderDetailBean.setGs_deliver_goods_counts(tDetailCount);
					tOrderDetailBean.setGs_deliver_goods_id(xmlDocument.getValueNoNull("SendCode"));
					tOrderDetailBean.setGs_deliveryman(xmlDocument.getValueNoNull("SendPerson"));
					tOrderDetailBean.setGs_deliver_goods_phone(xmlDocument.getValueNoNull("SendTel"));
					tOrderDetailBean.setGs_deliver_goods_confirm_date(xmlDocument.getValueNoNull("SendTime").substring(0,10));
					tOrderDetailBean.setGs_deliver_goods_confirm_time(xmlDocument.getValueNoNull("SendTime").substring(10));
					tOrderDetailBean.setGs_deliver_goods_explain(xmlDocument.getValueNoNull("SendRemark"));
					tOrderDetailBean.setGs_lastmodify_contents("接收到交易平台传来的订货明细送货确认信息");
					tOrderDetailBean.setGs_lastmodify_date(getCurDT());
					tOrderDetailBean.setGs_lastmodifer_id("交易平台");
					tempList.add(tOrderDetailBean);
				}else{
					res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99); // 交易结果 - 成功返回
					res.set("ErrorDesc", "订单明细编号不存在");
					return res;
				}
			}
			boolean tFlag = false;
			for(int i=0; i<tempList.size();i++){
				GSOrderDetailBean tOrderDetailBean = (GSOrderDetailBean)tempList.get(i);
				tFlag = tOrderDetailDAO.updateOrderDetailStatus(tOrderDetailBean,conn,GSBaseConstants.ORDER_ORDERING);
				if(tFlag){
					continue;
				}else{
					GSLogWriter.debug(this,"修改订单明细状态为送货确认失败");
					conn.rollback();
					break;
				}
			}
			if(tFlag){
				res.set("ResultCode", APEPResponse.RESULT_SUCCESS);
				res.set("ErrorDesc", "");
				conn.commit();
			}else{
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", "修改订单明细状态为送货确认失败");
			}
		} catch (Exception e) {
			if (e instanceof APEPServiceException) {
				APEPServiceException tAPEPException = (APEPServiceException) e;
				res.set("ResultCode", tAPEPException.getCode());
				res.set("ErrorDesc", e.getMessage());
			} else {
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", e.getMessage());
				LogWriter.warn("ConnectProcessorV1::processCreateOrder() - " + e.getLocalizedMessage());
			}
		}finally{
			GSBaseDAO.close(conn);
		}
		return res;
	}
	//订单拒绝
	public APEPResponse processRefuseOrder(APEPRequest req){
		GSOrderDetailDAO tOrderDetailDAO=new GSOrderDetailDAO();
		Connection conn=tOrderDetailDAO.getConnetion();
		APEPResponse res=new APEPResponse();
		try {
			conn.setAutoCommit(false);
			SampleXMLDocument xmlDocument=new SampleXMLDocument(req.toXml());
			ArrayList tList= xmlDocument.getDocuments(ROOT);
			ArrayList tempList = new ArrayList();
			for (int i = 0; i <tList.size(); i++) {
				xmlDocument=(SampleXMLDocument) tList.get(i);
				String orderDetailId=xmlDocument.getValueNoNull("CusOrderItemCode");
				//根据传入的每条订单明细送货确认信息的订单编号查询订单明细
				GSOrderDetailBean tOrderDetailBean = tOrderDetailDAO.queryOrderDetailByDetailId(conn,orderDetailId);
				if(tOrderDetailBean != null){
					tOrderDetailBean.setGs_reject_cause(StringUtils.escapeXML(xmlDocument.getValueNoNull("RefuseReason")));
					tOrderDetailBean.setGs_reject_date(xmlDocument.getValueNoNull("RefuseTime").substring(0,10));
					tOrderDetailBean.setGs_reject_time(xmlDocument.getValueNoNull("RefuseTime").substring(10));
					tOrderDetailBean.setGs_order_detail_status(GSBaseConstants.ORDER_ORDER_REJECT);
					tOrderDetailBean.setGs_lastmodifer_id("交易平台");
					tOrderDetailBean.setGs_lastmodify_contents("接收到交易平台传来的订货明细送货拒绝信息");
					tOrderDetailBean.setGs_lastmodify_date(getCurDT());
					tempList.add(tOrderDetailBean);
				}else{
					res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99); // 交易结果 - 成功返回
					res.set("ErrorDesc", "订单明细编号不存在");
					return res;
				}
			}
			boolean tFlag = false;
			for(int i=0; i<tempList.size();i++){
				GSOrderDetailBean tOrderDetailBean = (GSOrderDetailBean)tempList.get(i);
				tFlag = tOrderDetailDAO.updateOrderDetailStatus(tOrderDetailBean,conn,GSBaseConstants.ORDER_ORDERING);
				if(tFlag){
					continue;
				}else{
					GSLogWriter.debug(this,"修改订单明细状态为送货拒绝失败");
					conn.rollback();
					break;
				}
			}
			if(tFlag){
				res.set("ResultCode", APEPResponse.RESULT_SUCCESS);
				res.set("ErrorDesc", "");
				conn.commit();
			}else{
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", "修改订单明细状态为送货拒绝失败");
			}
		} catch (Exception e) {
			if (e instanceof APEPServiceException) {
				APEPServiceException tAPEPException = (APEPServiceException) e;
				res.set("ResultCode", tAPEPException.getCode());
				res.set("ErrorDesc", e.getMessage());
			} else {
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", e.getMessage());
				LogWriter.warn("ConnectProcessorV1::processCreateOrder() - " + e.getLocalizedMessage());
			}
		}finally{
			GSBaseDAO.close(conn);
		}
		return res;
	}
	//查询库存信息
	public APEPResponse queryWarehouseApep(APEPRequest req){
		APEPResponse res = new APEPResponse();
		try {
			SampleXMLDocument xmlDocument=new SampleXMLDocument(req.toXml());
			String providerCode= xmlDocument.getValueNoNull("ProviderCode");
			String[] porductIds=null;
			if(!"".equals(xmlDocument.getValueNoNull("ProductIds"))){
				porductIds=xmlDocument.getValueNoNull("ProductIds").split(",");
			}
			String isWarning=xmlDocument.getValueNoNull("IsWarning");
			String pageSize=xmlDocument.getValueNoNull("PageSize");
			String curentPage=xmlDocument.getValueNoNull("CurentPage");
			ArrayList tList=pdao.getProductStorageListPage(providerCode, porductIds, isWarning, pageSize, curentPage);
			int count=pdao.getProductStorageCount(providerCode, porductIds, isWarning);
			String data=productStorageToJson(count, pageSize, curentPage, tList);
			res.set("Data",data);
			res.setResultCode(APEPResponse.RESULT_SUCCESS);
			res.setErrorDesc("");
		} catch (Exception e) {
			if (e instanceof APEPServiceException) {
				APEPServiceException tAPEPException = (APEPServiceException) e;
				res.set("ResultCode", tAPEPException.getCode());
				res.set("ErrorDesc", e.getMessage());
			} else {
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", e.getMessage());
				LogWriter.warn("ConnectProcessorV1::processCreateOrder() - " + e.getLocalizedMessage());
			}
		} finally {
			// TODO:清理及释放代码
		}
		return res;
	}
	//add leo.gan 2010-05-19 查询供应产品信息

	public APEPResponse queryProductApep(APEPRequest req){
		APEPResponse res = new APEPResponse();
		try {
			SampleXMLDocument xmlDocument=new SampleXMLDocument(req.toXml());
			String providerCode= xmlDocument.getValueNoNull("ProviderCode");
			String[] porductIds=null;
			if(!"".equals(xmlDocument.getValueNoNull("ProductIds"))){
				porductIds=xmlDocument.getValueNoNull("ProductIds").split(",");
			}
			String pageSize=xmlDocument.getValueNoNull("PageSize");
			String curentPage=xmlDocument.getValueNoNull("CurentPage");
			ArrayList tList=pdao.getProductsListPage(providerCode, porductIds,  pageSize, curentPage);
			int count=pdao.getProductsCount(providerCode, porductIds);
			String data=productsToJson(count, pageSize, curentPage, tList);
			res.set("Data",data);
			res.setResultCode(APEPResponse.RESULT_SUCCESS);
			res.setErrorDesc("");
		} catch (Exception e) {
			if (e instanceof APEPServiceException) {
				APEPServiceException tAPEPException = (APEPServiceException) e;
				res.set("ResultCode", tAPEPException.getCode());
				res.set("ErrorDesc", e.getMessage());
			} else {
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", e.getMessage());
				LogWriter.warn("GSCommunicationApep::queryProductApep() - " + e.getLocalizedMessage());
			}
		} finally {
			// TODO:清理及释放代码
		}
		return res;
	}
	/**
	 * 将供应产品结果封装为json
	 * **/
	public String productsToJson(int count,String pagesize,String pageno,ArrayList tList) throws Exception {
		StringBuffer result=new StringBuffer(1048576);
		result.append("{"+SIGN+"TotalSize"+SIGN+":"+SIGN+count+SIGN+",");
		result.append(SIGN+"PageSize"+SIGN+":"+SIGN+pagesize+SIGN+",");
		result.append(SIGN+"CurentPage"+SIGN+":"+SIGN+pageno+SIGN+",");
		result.append(SIGN+"Data"+SIGN+":[");
		for (int i = 0; i < tList.size(); i++) {
			GSProductApepBean bean=(GSProductApepBean) tList.get(i);
			result.append("{");
			result.append(SIGN+"ProductID"+SIGN+":"+SIGN+bean.getApep_code()+SIGN+",");
			result.append(SIGN+"ProductName"+SIGN+":"+SIGN+StringUtils.escapeXML(bean.getName())+SIGN+",");
			result.append(SIGN+"BalanceMode"+SIGN+":"+SIGN+DefineDic.getBalanceMode(bean.getBalance_mode())+SIGN+",");
			result.append(SIGN+"CusItemCode"+SIGN+":"+SIGN+bean.getConcentricity_code()+SIGN+",");
			result.append(SIGN+"ProductModel"+SIGN+":"+SIGN+StringUtils.escapeXML(bean.getModel())+SIGN+",");
			result.append(SIGN+"ProductSpec"+SIGN+":"+SIGN+StringUtils.escapeXML(bean.getSpec())+SIGN+",");
			result.append(SIGN+"PorductTitle"+SIGN+":"+SIGN+StringUtils.escapeXML(bean.getProduct_brand())+SIGN+",");
			result.append(SIGN+"Manufaturer"+SIGN+":"+SIGN+StringUtils.escapeXML(bean.getManufaturer())+SIGN+",");
			result.append(SIGN+"Unit"+SIGN+":"+SIGN+bean.getProduct_unit()+SIGN+",");
			result.append(SIGN+"Price"+SIGN+":"+SIGN+bean.getUnit_price()+SIGN+",");
			result.append(SIGN+"QUALITY_ASSURE_PERIOD"+SIGN+":"+SIGN+bean.getQuality_assure_period()+SIGN+",");
			result.append(SIGN+"ADAPT_MACHINE_TYPE"+SIGN+":"+SIGN+bean.getAdapt_machine_type()+SIGN+",");
			result.append(SIGN + "DRAWING_PIECE_CODE" + SIGN + ":" + SIGN + bean.getDrawingPieceCode() + SIGN);
			if(i==tList.size()-1){
				result.append("}");
			}else{
				result.append("},");
			}
		}
		result.append("]}");
		String unzippedStr = result.toString().replaceAll("[\\r\\n]+","");


		String zippedStr = Base64.encode(GZIP.zip(unzippedStr.getBytes()));
		return zippedStr;

	}
	/**
	 * 查询供应产品下载为excel
	 * **/
	public APEPResponse queryProductsApepToExcel(APEPRequest req){
		APEPResponse res = new APEPResponse();
		try {
			SampleXMLDocument xmlDocument=new SampleXMLDocument(req.toXml());
			String providerCode= xmlDocument.getValueNoNull("ProviderCode");
			String[] porductIds=null;
			if(!"".equals(xmlDocument.getValueNoNull("ProductIds"))){
				porductIds=xmlDocument.getValueNoNull("ProductIds").split(",");
			}

			int count=pdao.getProductsCount(providerCode, porductIds);
			//            int maxcount=Integer.parseInt(GSSystemConfigurator.getParam("gs.download.count"));
			//            if (count>maxcount) {
			//                res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
			//                res.set("ErrorDesc","下载数据超过最大行数："+maxcount);
			//                return res;
			//            }
			ArrayList tList=pdao.getProductsList(providerCode, porductIds);
			String excel=productsToJson(count,String.valueOf(count),"1",tList);
			res.set("Data",excel);
			res.setResultCode(APEPResponse.RESULT_SUCCESS);
			res.setErrorDesc("");
		} catch (Exception e) {
			if (e instanceof APEPServiceException) {
				APEPServiceException tAPEPException = (APEPServiceException) e;
				res.set("ResultCode", tAPEPException.getCode());
				res.set("ErrorDesc", e.getMessage());
			} else {
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", e.getMessage());
				LogWriter.warn("GSCommunicationApep::queryProductsApepToExcel() - " + e.getLocalizedMessage());
			}
		} finally {

		}
		return res;
	}

	// add end

	//查询出入库信息
	public APEPResponse queryWarehousing(APEPRequest req){
		APEPResponse res = new APEPResponse();
		GSInOrOutStorageBean inOrOutBean=new GSInOrOutStorageBean();
		try {
			SampleXMLDocument xmlDocument=new SampleXMLDocument(req.toXml());
			String providerCode= xmlDocument.getValueNoNull("ProviderCode");
			inOrOutBean.setProviderCode(providerCode);
			inOrOutBean.setProductIds(xmlDocument.getValueNoNull("ProductIds"));
			inOrOutBean.setBeginDate(xmlDocument.getValueNoNull("BeginDate"));
			inOrOutBean.setEndDate(xmlDocument.getValueNoNull("EndDate"));
			inOrOutBean.setType(xmlDocument.getValueNoNull("Type"));
			String mode="";
			if("压库".equals(xmlDocument.getValueNoNull("BalanceMode"))){
				mode=GSBaseConstants.BALANCE_MODE1;
			}else if("买断".equals(xmlDocument.getValueNoNull("BalanceMode"))){
				mode=GSBaseConstants.BALANCE_MODE2;
			}
			inOrOutBean.setBalanceMode(mode);
			String pageSize=xmlDocument.getValueNoNull("PageSize");
			String curentPage=xmlDocument.getValueNoNull("CurentPage");
			ArrayList tList=pdao.getInOrOutStorageListPage(inOrOutBean, Integer.parseInt(curentPage), Integer.parseInt(pageSize));
			int count=pdao.getInOrOutStorageCount(inOrOutBean);
			String data=inOrOutToJson(count, pageSize, curentPage, tList);
			res.set("Data",data);
			res.setResultCode(APEPResponse.RESULT_SUCCESS);
			res.setErrorDesc("");
		} catch (Exception e) {
			if (e instanceof APEPServiceException) {
				APEPServiceException tAPEPException = (APEPServiceException) e;
				res.set("ResultCode", tAPEPException.getCode());
				res.set("ErrorDesc", e.getMessage());
			} else {
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", e.getMessage());
				LogWriter.warn("ConnectProcessorV1::processCreateOrder() - " + e.getLocalizedMessage());
			}
		} finally {
			// TODO:清理及释放代码
		}
		return res;
	}
	//交货确认
	public APEPResponse processFinishOrder(APEPRequest req){
		GSOrderDetailDAO tOrderDetailDAO=new GSOrderDetailDAO();
		Connection conn=tOrderDetailDAO.getConnetion();
		APEPResponse res=new APEPResponse();
		try {
			conn.setAutoCommit(false);
			SampleXMLDocument xmlDocument=new SampleXMLDocument(req.toXml());
			ArrayList tList= xmlDocument.getDocuments(ROOT);
			ArrayList tempList = new ArrayList();
			for (int i = 0; i <tList.size(); i++) {
				xmlDocument=(SampleXMLDocument) tList.get(i);
				String orderDetailId=xmlDocument.getValueNoNull("CusOrderItemCode");
				//根据传入的每条订单明细送货确认信息的订单编号查询订单明细
				GSOrderDetailBean tOrderDetailBean = tOrderDetailDAO.queryOrderDetailByDetailId(conn,orderDetailId);
				if(tOrderDetailBean != null){
					tOrderDetailBean.setGs_deliver_goods_id(xmlDocument.getValueNoNull("SendCode"));
					tOrderDetailBean.setGs_signin_date(xmlDocument.getValueNoNull("ReceiveTime").substring(0,10));
					tOrderDetailBean.setGs_signin_time(xmlDocument.getValueNoNull("ReceiveTime").substring(10));
					tOrderDetailBean.setGs_signin_count(Double.parseDouble(xmlDocument.getValueNoNull("ReceiveQty")));
					tOrderDetailBean.setGs_signin_person(xmlDocument.getValueNoNull("ReceivePerson"));
					tOrderDetailBean.setGs_signin_explain(xmlDocument.getValueNoNull("ReceiveRemark"));
					tOrderDetailBean.setGs_goods_unitprice(Double.parseDouble(xmlDocument.getValueNoNull("SurePrice")));
					tOrderDetailBean.setGs_order_detail_status(GSBaseConstants.ORDER_DELIVERY_CONFIRM);
					tOrderDetailBean.setGs_lastmodify_contents("接收到交易平台传来的交货确认明细信息");
					tOrderDetailBean.setGs_lastmodifer_id("交易平台");
					tOrderDetailBean.setGs_lastmodify_date(getCurDT());
					tempList.add(tOrderDetailBean);
				}else{
					res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99); // 交易结果 - 成功返回
					res.set("ErrorDesc", "订单明细编号不存在");
					return res;
				}
			}
			boolean tFlag = false;
			for(int i=0; i<tempList.size();i++){
				GSOrderDetailBean tOrderDetailBean = (GSOrderDetailBean)tempList.get(i);
				tFlag = tOrderDetailDAO.updateOrderDetailStatus(tOrderDetailBean,conn,GSBaseConstants.ORDER_ORDER_CONFIRM);
				if(tFlag){
					continue;
				}else{
					GSLogWriter.debug(this,"修改订单明细状态为交货确认失败");
					conn.rollback();
					break;
				}
			}
			if(tFlag){
				res.set("ResultCode", APEPResponse.RESULT_SUCCESS);
				res.set("ErrorDesc", "");
				conn.commit();
			}else{
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", "修改订单明细状态为交货确认失败");
			}
		} catch (Exception e) {
			if (e instanceof APEPServiceException) {
				APEPServiceException tAPEPException = (APEPServiceException) e;
				res.set("ResultCode", tAPEPException.getCode());
				res.set("ErrorDesc", e.getMessage());
			} else {
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", e.getMessage());
				LogWriter.warn("ConnectProcessorV1::processCreateOrder() - " + e.getLocalizedMessage());
			}
		}finally{
			GSBaseDAO.close(conn);
		}
		return res;
	}
	//申请取消的处理
	public APEPResponse processDisposeCancelOrder(APEPRequest req){
		GSOrderDetailDAO tOrderDetailDAO=new GSOrderDetailDAO();
		Connection conn=tOrderDetailDAO.getConnetion();
		APEPResponse res=new APEPResponse();
		try {
			conn.setAutoCommit(false);
			SampleXMLDocument xmlDocument=new SampleXMLDocument(req.toXml());
			ArrayList tList= xmlDocument.getDocuments(ROOT);
			ArrayList tempList = new ArrayList();
			for (int i = 0; i <tList.size(); i++) {
				xmlDocument=(SampleXMLDocument) tList.get(i);
				String orderDetailId=xmlDocument.getValueNoNull("CusOrderItemCode");
				//根据传入的每条订单明细送货确认信息的订单编号查询订单明细
				GSOrderDetailBean tOrderDetailBean = tOrderDetailDAO.queryOrderDetailByDetailId(conn,orderDetailId);
				if(tOrderDetailBean != null){
					tOrderDetailBean.setDisposeCancelReason(xmlDocument.getValueNoNull("DisposeCancelReason"));
					tOrderDetailBean.setDisposeCancelPerson(xmlDocument.getValueNoNull("DisposeCancelPerson"));
					tOrderDetailBean.setDisposeCancelTime(xmlDocument.getValueNoNull("DisposeCancelTime"));
					//如果取消申请经交易平台处理同意，则集中采购系统标记为作废，否则为已确认
					if (xmlDocument.getValueNoNull("DisposeCancelStatus").equalsIgnoreCase("Y")) {
						tOrderDetailBean.setGs_order_detail_status(GSBaseConstants.ORDER_BLANK_OUT);
					} else {
						tOrderDetailBean.setGs_order_detail_status(GSBaseConstants.ORDER_ORDER_CONFIRM);
					}

					tOrderDetailBean.setGs_lastmodify_contents("接收到交易平台传来的取消申请的处理");
					tOrderDetailBean.setGs_lastmodifer_id("交易平台");
					tOrderDetailBean.setGs_lastmodify_date(getCurDT());
					tempList.add(tOrderDetailBean);
				}else{
					res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99); // 交易结果 - 成功返回
					res.set("ErrorDesc", "订单明细编号不存在");
					return res;
				}
			}
			boolean tFlag = false;
			for(int i=0; i<tempList.size();i++){
				GSOrderDetailBean tOrderDetailBean = (GSOrderDetailBean)tempList.get(i);
				tFlag = tOrderDetailDAO.updateOrderDetailStatus(tOrderDetailBean,conn,GSBaseConstants.ORDER_ORDER_CONFIRM);
				if(tFlag){
					continue;
				}else{
					GSLogWriter.debug(this,"修改订单明细状态为交货确认失败");
					conn.rollback();
					break;
				}
			}
			if(tFlag){
				res.set("ResultCode", APEPResponse.RESULT_SUCCESS);
				res.set("ErrorDesc", "");
				conn.commit();
			}else{
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", "修改订单明细状态为取消订单失败");
			}
		} catch (Exception e) {
			if (e instanceof APEPServiceException) {
				APEPServiceException tAPEPException = (APEPServiceException) e;
				res.set("ResultCode", tAPEPException.getCode());
				res.set("ErrorDesc", e.getMessage());
			} else {
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", e.getMessage());
				LogWriter.warn("ConnectProcessorV1::processCreateOrder() - " + e.getLocalizedMessage());
			}
		}finally{
			GSBaseDAO.close(conn);
		}
		return res;
	}
	//申请作废处理
	public APEPResponse processDeposeOrder(APEPRequest req){
		GSOrderDetailDAO tOrderDetailDAO=new GSOrderDetailDAO();
		Connection conn=tOrderDetailDAO.getConnetion();
		APEPResponse res=new APEPResponse();
		try {
			conn.setAutoCommit(false);
			SampleXMLDocument xmlDocument=new SampleXMLDocument(req.toXml());
			ArrayList tList= xmlDocument.getDocuments(ROOT);
			ArrayList tempList = new ArrayList();
			for (int i = 0; i <tList.size(); i++) {
				xmlDocument=(SampleXMLDocument) tList.get(i);
				String orderDetailId=xmlDocument.getValueNoNull("CusOrderItemCode");
				//根据传入的每条订单明细送货确认信息的订单编号查询订单明细
				GSOrderDetailBean tOrderDetailBean = tOrderDetailDAO.queryOrderDetailByDetailId(conn,orderDetailId);
				if(tOrderDetailBean != null){
					tOrderDetailBean.setGs_order_detail_status(GSBaseConstants.ORDER_BLANK_OUT);
					tOrderDetailBean.setGs_lastmodify_contents(StringUtils.escapeXML(xmlDocument.getValueNoNull("DeposeReason")));
					tOrderDetailBean.setGs_lastmodifer_id(xmlDocument.getValueNoNull("DeposeUser"));
					tOrderDetailBean.setGs_lastmodify_date(xmlDocument.getValueNoNull("DeposeTime"));
					tempList.add(tOrderDetailBean);
				}else{
					res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99); // 交易结果 - 成功返回
					res.set("ErrorDesc", "订单明细编号不存在");
					return res;
				}
			}
			boolean tFlag = false;
			for(int i=0; i<tempList.size();i++){
				GSOrderDetailBean tOrderDetailBean = (GSOrderDetailBean)tempList.get(i);
				tFlag = tOrderDetailDAO.updateOrderDetailStatus(tOrderDetailBean,conn,GSBaseConstants.ORDER_BLANK_OUT);
				if(tFlag){
					continue;
				}else{
					GSLogWriter.debug(this,"修改订单明细状态为作废失败");
					conn.rollback();
					break;
				}
			}
			if(tFlag){
				res.set("ResultCode", APEPResponse.RESULT_SUCCESS);
				res.set("ErrorDesc", "");
				conn.commit();
			}else{
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", "修改订单明细状态为作废失败");
			}
		} catch (Exception e) {
			if (e instanceof APEPServiceException) {
				APEPServiceException tAPEPException = (APEPServiceException) e;
				res.set("ResultCode", tAPEPException.getCode());
				res.set("ErrorDesc", e.getMessage());
			} else {
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", e.getMessage());
				LogWriter.warn("ConnectProcessorV1::processCreateOrder() - " + e.getLocalizedMessage());
			}
		}finally{
			GSBaseDAO.close(conn);
		}
		return res;
	}
	/**
	 * 接收补货申请
	 * */
	public APEPResponse processApplyProduct(APEPRequest req){
		GSManufactureDAO mdao=new GSManufactureDAO();
		GSOrderApplyDao applyDao=new GSOrderApplyDao();
		APEPResponse res=new APEPResponse();
		DBConnection dbcon=null;
		try {
			dbcon=DBConnection.open(true);
			SampleXMLDocument xmlDocument=new SampleXMLDocument(req.toXml());
			ArrayList tList= xmlDocument.getDocuments(ROOT);
			ArrayList tempList = new ArrayList();
			for (int i = 0; i <tList.size(); i++) {
				xmlDocument=(SampleXMLDocument) tList.get(i);
				String applyCode=xmlDocument.getValueNoNull("ApplyCode");//补货单号
				//产品验证
				String apepcode=xmlDocument.getValueNoNull("ProductID");
				String conid=xmlDocument.getValueNoNull("CusItemCode");
				GSManufactureBean tGSManufactureBean=mdao.getManufactureDetail(apepcode, conid);
				if(tGSManufactureBean==null){
					res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99); // 交易结果 - 成功返回
					res.set("ErrorDesc", "产品信息不存在");
					return res;
				}
				//仓库验证
				String warecode=xmlDocument.getValueNoNull("WareHouseNo");
				Warehouse ware=Warehouse.fetchByCode(dbcon, warecode);
				if(ware==null){
					res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99); // 交易结果 - 成功返回
					res.set("ErrorDesc", "仓库信息不存在");
					return res;
				}
				GSOrderApplyBean applybean=new GSOrderApplyBean();
				applybean.setApply_code(applyCode);
				applybean.setApep_code(apepcode);
				applybean.setConcentricity_code(conid);
				applybean.setWarehouse_code(warecode);
				applybean.setWarehouse_name(xmlDocument.getValueNoNull("WareHouseName"));
				applybean.setProvider_code(xmlDocument.getValueNoNull("ProviderCode"));
				double count=Double.parseDouble(xmlDocument.getValueNoNull("SendQty"));
				applybean.setSend_count(count);
				applybean.setSend_date(xmlDocument.getValueNoNull("SendDate"));
				applybean.setSend_date(xmlDocument.getValueNoNull("SendTime"));
				applybean.setApply_status(GSBaseConstants.ORDER_SUPPLY_APPLY);
				applybean.setLast_update_user_id(ConstDefine.T_USER_SUPERADMIN_ID);
				applybean.setLast_update_user(ConstDefine.T_USER_SUPERADMIN_CODE);
				applybean.setLast_update_time(getCurDT());
				applybean.setApply_date(getCurDT());
				tempList.add(applybean);
			}
			boolean tFlag = false;
			tFlag=applyDao.saveOrderApply(tempList);
			if(tFlag){
				res.set("ResultCode", APEPResponse.RESULT_SUCCESS);
				res.set("ErrorDesc", "");
			}else{
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", "写入补货申请失败");
			}
		} catch (Exception e) {
			if (e instanceof APEPServiceException) {
				APEPServiceException tAPEPException = (APEPServiceException) e;
				res.set("ResultCode", tAPEPException.getCode());
				res.set("ErrorDesc", e.getMessage());
			} else {
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", e.getMessage());
				LogWriter.warn("ConnectProcessorV1::processCreateOrder() - " + e.getLocalizedMessage());
			}
		}finally{
			try {
				dbcon.close();
			} catch (Exception e) {
			}
		}
		return res;
	}
	/**
	 * 查询库存下载为excel
	 * **/
	public APEPResponse queryWarehouseApepToExcel(APEPRequest req){
		APEPResponse res = new APEPResponse();
		try {
			SampleXMLDocument xmlDocument=new SampleXMLDocument(req.toXml());
			String providerCode= xmlDocument.getValueNoNull("ProviderCode");
			String[] porductIds=null;
			if(!"".equals(xmlDocument.getValueNoNull("ProductIds"))){
				porductIds=xmlDocument.getValueNoNull("ProductIds").split(",");
			}
			String isWarning=xmlDocument.getValueNoNull("IsWarning");
			int count=pdao.getProductStorageCount(providerCode, porductIds, isWarning);
			int maxcount=Integer.parseInt(GSSystemConfigurator.getParam("gs.download.count"));
			if (count>maxcount) {
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc","下载数据超过最大行数："+maxcount);
				return res;
			}
			ArrayList tList=pdao.getProductStorageList(providerCode, porductIds, isWarning);
			String excel=productStorageToJson(count,String.valueOf(count),"1",tList);
			res.set("Data",excel);
			res.setResultCode(APEPResponse.RESULT_SUCCESS);
			res.setErrorDesc("");
		} catch (Exception e) {
			if (e instanceof APEPServiceException) {
				APEPServiceException tAPEPException = (APEPServiceException) e;
				res.set("ResultCode", tAPEPException.getCode());
				res.set("ErrorDesc", e.getMessage());
			} else {
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", e.getMessage());
				LogWriter.warn("ConnectProcessorV1::processCreateOrder() - " + e.getLocalizedMessage());
			}
		} finally {

		}
		return res;
	}
	/**
	 * 查询出入库下载为excel
	 * **/
	public APEPResponse queryWarehousingApepToExcel(APEPRequest req){
		APEPResponse res = new APEPResponse();
		GSInOrOutStorageBean inOrOutBean=new GSInOrOutStorageBean();
		try {
			SampleXMLDocument xmlDocument=new SampleXMLDocument(req.toXml());
			String providerCode= xmlDocument.getValueNoNull("ProviderCode");
			inOrOutBean.setProviderCode(providerCode);
			inOrOutBean.setProductIds(xmlDocument.getValueNoNull("ProductIds"));
			inOrOutBean.setBeginDate(xmlDocument.getValueNoNull("BeginDate"));
			inOrOutBean.setEndDate(xmlDocument.getValueNoNull("EndDate"));
			inOrOutBean.setType(xmlDocument.getValueNoNull("Type"));
			String mode="";
			if("压库".equals(xmlDocument.getValueNoNull("BalanceMode"))){
				mode=GSBaseConstants.BALANCE_MODE1;
			}else if("买断".equals(xmlDocument.getValueNoNull("BalanceMode"))){
				mode=GSBaseConstants.BALANCE_MODE2;
			}
			inOrOutBean.setBalanceMode(mode);
			int count=pdao.getInOrOutStorageCount(inOrOutBean);
			int maxcount=Integer.parseInt(GSSystemConfigurator.getParam("gs.download.count"));
			if (count>maxcount) {
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc","下载数据超过最大行数："+maxcount);
				return res;
			}
			ArrayList tList=pdao.getInOrOutStorageList(inOrOutBean);
			String excel=inOrOutToJson(count, String.valueOf(count), "1", tList);
			res.set("Data",excel);
			res.setResultCode(APEPResponse.RESULT_SUCCESS);
			res.setErrorDesc("");
		} catch (Exception e) {
			if (e instanceof APEPServiceException) {
				APEPServiceException tAPEPException = (APEPServiceException) e;
				res.set("ResultCode", tAPEPException.getCode());
				res.set("ErrorDesc", e.getMessage());
			} else {
				res.set("ResultCode", APEPServiceException.APEP_EXC_CODE_99);
				res.set("ErrorDesc", e.getMessage());
				LogWriter.warn("ConnectProcessorV1::queryWarehousingApepToExcel() - " + e.getLocalizedMessage());
			}
		} finally {
			// TODO:清理及释放代码
		}
		return res;
	}
	/**
	 * 将库存结果封装为json
	 * **/
	public String productStorageToJson(int count,String pagesize,String pageno,ArrayList tList){
		StringBuffer result=new StringBuffer(1048576);
		result.append("{"+SIGN+"TotalSize"+SIGN+":"+SIGN+count+SIGN+",");
		result.append(SIGN+"PageSize"+SIGN+":"+SIGN+pagesize+SIGN+",");
		result.append(SIGN+"CurentPage"+SIGN+":"+SIGN+pageno+SIGN+",");
		result.append(SIGN+"Data"+SIGN+":[");
		for (int i = 0; i < tList.size(); i++) {
			GSProductStorageApepBean bean=(GSProductStorageApepBean) tList.get(i);
			result.append("{");
			result.append(SIGN+"ProductID"+SIGN+":"+SIGN+bean.getApep_code()+SIGN+",");
			result.append(SIGN+"ProductName"+SIGN+":"+SIGN+StringUtils.escapeXML(bean.getName())+SIGN+",");
			result.append(SIGN+"CusItemCode"+SIGN+":"+SIGN+bean.getConcentricity_code()+SIGN+",");
			result.append(SIGN+"ProductModel"+SIGN+":"+SIGN+StringUtils.escapeXML(bean.getModel())+SIGN+",");
			result.append(SIGN+"ProductSpec"+SIGN+":"+SIGN+StringUtils.escapeXML(bean.getSpec())+SIGN+",");
			result.append(SIGN+"PorductTitle"+SIGN+":"+SIGN+StringUtils.escapeXML(bean.getProduct_brand())+SIGN+",");
			result.append(SIGN+"Manufaturer"+SIGN+":"+SIGN+StringUtils.escapeXML(bean.getManufaturer())+SIGN+",");
			result.append(SIGN+"WareHouseNo"+SIGN+":"+SIGN+bean.getWarehouse_code()+SIGN+",");
			result.append(SIGN+"WareHouseName"+SIGN+":"+SIGN+bean.getWarehouse_name()+SIGN+",");
			result.append(SIGN+"Qty"+SIGN+":"+SIGN+bean.getProduct_count()+SIGN+",");
			result.append(SIGN+"Unit"+SIGN+":"+SIGN+bean.getProduct_unit()+SIGN+",");
			result.append(SIGN+"PQty"+SIGN+":"+SIGN+bean.getMin_count()+SIGN+",");
			result.append(SIGN+"BalanceMode"+SIGN+":"+SIGN+DefineDic.getBalanceMode(bean.getBalance_mode())+SIGN);
			if(i==tList.size()-1){
				result.append("}");
			}else{
				result.append("},");
			}
		}
		result.append("]}");
		return result.toString().replaceAll("[\\r\\n]+","");
	}

	/**
	 * 将出入库结果封装为json
	 * **/
	public String inOrOutToJson(int count,String pagesize,String pageno,ArrayList tList){
		StringBuffer result=new StringBuffer(1048576);
		result.append("{"+SIGN+"TotalSize"+SIGN+":"+SIGN+count+SIGN+",");
		result.append(SIGN+"PageSize"+SIGN+":"+SIGN+pagesize+SIGN+",");
		result.append(SIGN+"CurentPage"+SIGN+":"+SIGN+pageno+SIGN+",");
		result.append(SIGN+"Data"+SIGN+":[");
		for (int i = 0; i < tList.size(); i++) {
			GSInOrOutStorageBean bean=(GSInOrOutStorageBean) tList.get(i);
			result.append("{");
			result.append(SIGN+"ProductID"+SIGN+":"+SIGN+bean.getProductId()+SIGN+",");
			result.append(SIGN+"ProductName"+SIGN+":"+SIGN+StringUtils.escapeXML(bean.getProductName())+SIGN+",");
			result.append(SIGN+"CusItemCode"+SIGN+":"+SIGN+bean.getConcentricityCode()+SIGN+",");
			result.append(SIGN+"WareHouseNo"+SIGN+":"+SIGN+bean.getWarehouseNo()+SIGN+",");
			result.append(SIGN+"WareHouseName"+SIGN+":"+SIGN+bean.getWarehouseName()+SIGN+",");
			result.append(SIGN+"ProductModel"+SIGN+":"+SIGN+StringUtils.escapeXML(bean.getProductModel())+SIGN+",");
			result.append(SIGN+"ProductSpec"+SIGN+":"+SIGN+StringUtils.escapeXML(bean.getProductSpec())+SIGN+",");
			result.append(SIGN+"PorductTitle"+SIGN+":"+SIGN+StringUtils.escapeXML(bean.getProductTitle())+SIGN+",");
			result.append(SIGN+"Manufaturer"+SIGN+":"+SIGN+StringUtils.escapeXML(bean.getManufaturer())+SIGN+",");
			result.append(SIGN+"BalanceMode"+SIGN+":"+SIGN+DefineDic.getBalanceMode(bean.getBalanceMode())+SIGN+",");
			result.append(SIGN+"Date"+SIGN+":"+SIGN+bean.getInoutdate()+SIGN+",");
			result.append(SIGN+"Qty"+SIGN+":"+SIGN+bean.getQty()+SIGN+",");
			result.append(SIGN+"Unit"+SIGN+":"+SIGN+bean.getUnit()+SIGN);
			if(i==tList.size()-1){
				result.append("}");
			}else{
				result.append("},");
			}
		}
		result.append("]}");
		return result.toString().replaceAll("[\\r\\n]+","");
	}
	/**
	 * 解析返回信息
	 * **/
	public static String[] parseReturnXml(String tXml){
		String[] result=new String[2];
		SampleXMLDocument xmlDocument=new SampleXMLDocument(tXml);
		result[0]=xmlDocument.getValueNoNull("ResultCode");
		if (GSBaseConstants.APEP_RETURN_SIGN_SUCCESS.equals(result[0])) {
			result[1]="订单处理成功！";
		}else{
			String[] res=xmlDocument.getValueNoNull("ErrorDesc").split("-");
			if(res.length>1){
				result[1]=res[1];
			}else{
				result[1]=res[0];
			}
		}
		return result;
	}
	/**
	 * 取得系统当前日期
	 * @return
	 */
	public static String getCurDT(){
		String tCurDT = "";
		try{
			tCurDT = DateUtil.formateDateTime(DateUtil.getCurrentTime("DT","AD"));
		}catch(Exception e){
			e.printStackTrace();
		}
		return tCurDT;
	}


	/*************请求交易平台**************/
	//测试接口
	public static String testToApep() throws Exception{
		APEPRequest req=new APEPRequest();
		req.setAction("testAction");
		SampleElement element=new SampleElement("test",CUSID);
		req.setData(element);
		GSApepClient cilent=new GSApepClient();
		APEPResponse res= cilent.sendToApep(req);
		String tRs=res.toXml();
		GSLogWriter.info(tRs);
		return tRs;
	}
}
